// pages/search/search.js
let util = require("../../utils/util.js");
let baseImgUrl = util.baseImgUrl;
let app = getApp();
let searchWords;
const getInf = (str, key) => str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    networkStatus: true,
    loadingStatus: false,//默认加载完成，true--正在加载中
    loadgif: util.imgUrl + "loading.gif",//加载gif图
    loading: util.imgUrl + "loading.png",
    none_network: util.imgUrl + "none_network.png",

    page:1,
    inputwords: "",
    articleNum: 0,
    shopNum: 0,
    goodsNum: 0,
    shut3: baseImgUrl+"shut3.png",
    search: baseImgUrl +"search.png",
    shutshow:false,//是否显示X号图片
    inputTxt: '',
    historyList:[
      {
        id:0,
        name:"痘痘肌肤"
      },
      {
        id: 0,
        name: "痘痘肌肤"
      },
      {
        id: 1,
        name: "面膜"
      },
      {
        id: 2,
        name: "爽肤水"
      },
      {
        id: 3,
        name: "今天的特价商品"
      }, {
        id: 4,
        name: "痘痘肌肤"
      },
      {
        id: 5,
        name: "痘痘肌肤"
      },
      {
        id: 6,
        name: "面膜"
      },
      {
        id: 7,
        name: "爽肤水"
      },
      {
        id: 8,
        name: "今天的特价商品"
      },

    ],
     hotList: [{
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 1,
      name: "面膜"
    },
    {
      id: 2,
      name: "爽肤水"
    },
    {
      id: 3,
      name: "今天的特价商品"
    }, {
      id: 4,
      name: "痘痘肌肤"
    },
    {
      id: 5,
      name: "痘痘肌肤"
    },
    {
      id: 6,
      name: "面膜"
    },
    {
      id: 7,
      name: "爽肤水"
    },
    {
      id: 8,
      name: "今天的特价商品"
    },

    ],
    suggestList: [{
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 0,
      name: "痘痘肌肤"
    },
    {
      id: 1,
      name: "面膜"
    },
    {
      id: 2,
      name: "爽肤水"
    }
    ],
  },

  /***************点击模糊匹配的选项******************* */
  searchResult:function(){
      //跳转到新闻详情页面

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      loadingStatus: true
    })
    /*********判断网络状态******************/
    wx.getNetworkType({
      success: function (res) {
        var networkType = res.networkType;
        if (networkType == "none" || networkType == "unknown") {
          that.setData({
            networkStatus: false
          })
        } else {
          that.setData({
            networkStatus: true,

          })
        }
      }, fail: function (err) {
        console.log("获取网络失败" + err);
        that.setData({
          networkStatus: false
        })
      }
    })

    //展示历史搜索
    wx.request({
      url: util.souindex,    
      data: {
        openid: app.globalData.openid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        that.setData({
          'historyList': res.data.historyList,
          'hotList': res.data.hotList,
          loadingStatus: false
        })
      },
      fail: function (err) {
        console.log("搜索页onload报错" + err);
        that.setData({
          loadingStatus: true
        })
      }
    })
  
  },
  //搜索框失去焦点
  searchBlur:function(event){
      console.log(event.detail);
      searchWords=event.detail.value;
      var that=this;
      that.setData({
        searchWords:searchWords
      })
  },
  //搜索框输入时
  searchInput:function(event){
      var that=this;
      var inputwords=event.detail.value;//搜索框内输入的文字
    if (inputwords){
      that.setData({
        suggestflag:true,
        shutshow:true
      })
    }else{
      that.setData({
        suggestflag: false,
        shutshow: false
      })
    }

    

    // var data = that.data.listData;
    // var newData = that.data.listDataCopy;
    // for (var i = 0; i < data.length; i++) {
    //   var dic = data[i];
    //   var newDic = newData[i];
    //   var text = dic["title"];
    //   newDic["title"] = getInf(text, that.data.keyName);
    // }
    // console.log(newDic);
    // that.setData({
    //   listDataCopy: newData,
    //   inputwords: inputwords,
    // })

    //搜索类似名称
    wx.request({
      url: util.search_where,
      data: {
        key: inputwords,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res)
        that.setData({
          listDataCopy: res.data.suggestList,
          inputwords:inputwords,
          articleNum: 45,
          shopNum: 30,
          goodsNum: 50
        })
      },
      fail: function (err) {
        console.log("err" + err);
      }
    })
  },

  /*********点击删除关键字图片********** */
  clearKeywords:function(){
      var that=this;
      that.setData({
        inputwords: '',
        suggestflag: false,
        shutshow: false


      })
  },
  /********点击搜索按钮************ */
  searchBtn:function(e){
    var that=this;
    searchWords=that.data.inputwords//访问接口，传值用
    console.log("--------------------searchWords-----------------");
    console.log(searchWords);
    if (!searchWords || searchWords.trim == "" || searchWords=="none"){
      
      wx.showToast({
        title: '请输入关键字',
        icon: "none"
      })
    }else{
      wx.request({
        url: util.search_insert,
        data: {
          openid: app.globalData.openid,
          key: searchWords,
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded" //post
        },
        success: function (res) {
        },
        fail: function (err) {
          console.log("err" + err);
        }
      })
      //请求接口，跳转页面
      wx.navigateTo({
        url: '/pages/searchResult/searchResult?key=' + searchWords,
      })
    }
  },

  //点击热门
  searchBtn_remen: function (e) {
    var that = this;
    var searchwords = e.currentTarget.dataset.searchwords//访问接口，传值用
    //请求接口，跳转页面
    wx.navigateTo({
      url: '/pages/searchResult/searchResult?key=' + searchwords,
    })
  },

  /******清除历史记录***********/ 
  clearUp:function(){
    var that = this;
    wx.request({
      url: util.soudel,
      data: {
        openid: app.globalData.openid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res);
        that.setData({
          'historyList': res.data.historyList,
        })
      },
      fail: function (err) {
        console.log("err" + err);
      }
    })
  },
/*********** 换一批 *****************/
  huanyipi: function () {
    var that = this;
    that.data.page = that.data.page+1;
    wx.request({
      url: util.souhuan,
      data: {
        page: that.data.page,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res);
        that.setData({
          'hotList': res.data.hotList,
        })
      },
      fail: function (err) {
        console.log("err" + err);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: util.souindex,
      data: {
        openid: app.globalData.openid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        that.setData({
          'historyList': res.data.historyList,
          'hotList': res.data.hotList,
        })
      },
      fail: function (err) {
        console.log("err" + err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})